﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Pub.Controls
{
    public partial class AutoFilterFormTextBox : Form
    {
        public AutoFilterFormTextBox()
        {
            InitializeComponent();
        }
        public string columnname = "";
        public Type columntype=typeof(System.String);
        public string strvalue="";
        public string strvalue1 = "";
        public string strvalue2 = "";
        public string strreturnvalue = "";
        public string strreturnvalue1 = "";
        public string strreturnvalue2 = "";
        //判斷是否按了按鈕，如果按了按鈕，如果都爲空，那麼就變成選(全部)
        public bool clickbutton = false;
        //正則表達式
        private string pattern = @"^\d*.?\d*$";


        private void FormAutoFilterTextBox_Load(object sender, EventArgs e)
        {
            groupBox1.Text = columnname;
            if (columntype == typeof(System.String))
            {
                //label1.Visible = false;
                label2.Visible = false;
                textBox2.Visible = false;
                textBox3.Visible = false;
                dateTimePicker1.Visible = false;
                dateTimePicker2.Visible = false;
                label1.Text = "查找:";
                textBox1.Text = strvalue;
            }
            else if (columntype == typeof(System.DateTime))
            {
                textBox2.Visible = false;
                textBox3.Visible = false;
                textBox1.Visible = false;
                if (strvalue1 != "")
                {
                    try
                    {
                        dateTimePicker1.Value = Convert.ToDateTime(strvalue1);
                    }
                    catch
                    { 
                    }
                }
                if (strvalue2 != "")
                {
                    try
                    {
                        dateTimePicker2.Value = Convert.ToDateTime(strvalue2);
                    }
                    catch
                    {
                    }
                }
            }
            else
            {
                dateTimePicker1.Visible = false;
                dateTimePicker2.Visible = false;
                textBox1.Visible = false;
                textBox2.Text = strvalue1;
                textBox3.Text = strvalue2;
                this.Show();
                textBox2.Focus();
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            customOK();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      

        private void customOK()
        {
            strreturnvalue = textBox1.Text;
            if (columntype == typeof(System.DateTime))
            {
                strreturnvalue1 = dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
                strreturnvalue2 = dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                Match m1 = Regex.Match(textBox2.Text.ToString().Trim(), pattern);
                if (!m1.Success)
                {
                    textBox2.Focus();
                    MessageBox.Show("请填写数字!");
                    return;
                }
                Match m2 = Regex.Match(textBox3.Text.ToString().Trim(), pattern);
                if (!m2.Success)
                {
                    textBox3.Focus();
                    MessageBox.Show("请填写数字!");
                    return;
                }
                strreturnvalue1 = textBox2.Text.ToString().Trim();
                strreturnvalue2 = textBox3.Text.ToString().Trim();
            }
            clickbutton = true;
            this.Close();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                customOK();
            }
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox3.Focus();
               // customOK();
            }
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                customOK();
            }
        }

        private void dateTimePicker1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                customOK();
            }
        }

        private void dateTimePicker2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                customOK();
            }
        }
        

       
    }
}